package com.qctpos.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qctpos.crm.dto.DemoListDto;
import com.qctpos.crm.dto.PagerQueryDto;
import com.qctpos.crm.dto.UserLoginDto;
import com.qctpos.crm.dto.UserLoginResultDto;
import com.qctpos.crm.entity.Demo1;
import com.qctpos.crm.entity.Demo2;
import com.qctpos.crm.global.utils.DateHelper;
import com.qctpos.crm.global.utils.JwtTokenUtil;
import com.qctpos.crm.mapper.Demo1Mapper;
import com.qctpos.crm.mapper.Demo2Mapper;
import com.qctpos.crm.service.Demo1Service;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class Demo1ServiceImpl extends BaseServiceImpl<Demo1Mapper, Demo1> implements Demo1Service {

    @Autowired
    Demo2Mapper demo2Mappere;

    public IPage<Demo1>  GetPagerList(PagerQueryDto input){

        QueryWrapper<Demo1> queryWrapper = new QueryWrapper<>();
                queryWrapper.like("store_name","名称")
                        //.eq("facilitator_id",1)
                        .orderByAsc("id");
        var page = input.toPage(Demo1.class);
        return baseMapper.findPage(page,queryWrapper);
    }


    public IPage<DemoListDto> selectDemo1PagerList(PagerQueryDto input) {

        var page = input.toPage(DemoListDto.class);
        return baseMapper.selectDemo1PagerList(page, input);

    }


    /**
     * 测试事务 同时修改多张表 中间异常
     */
    @Transactional //开启事务  注解打在实现方法上
    public void  TestTransactional(){

        var store  = baseMapper.selectById(1);
        var fac = demo2Mappere.selectById(1);
        var date = new Date();
        store.setStoreName("新商户名"+ DateHelper.formatDate(date,"yyyy-MM-dd HH:mm:ss"));
        baseMapper.updateById(store);
        //var ss = 10/0;
        fac.setFacilitatorName("新商户名"+ DateHelper.formatDate(date,"yyyy-MM-dd HH:mm:ss"));
        demo2Mappere.updateById(fac);
    }

    /**
     * 登录
     */
    public UserLoginResultDto Login(UserLoginDto input) throws Exception {
        if(!input.getUserAccount().equals("qct")||!input.getPassword().equals("qct"))
        {
            throw new Exception("账号密码不正确");
        }
        UserLoginResultDto resualt = new UserLoginResultDto();
        Map<String,String> claims = new TreeMap <>();
        claims.put("userId","1");
        claims.put("userName","全城淘");
        var token = JwtTokenUtil.create(null,claims);
        resualt.setToken(token);
        resualt.setUser(input);
        return  resualt;

    }

    /**
     * 测试事务 同时修改多张表 中间异常
     */
    @Transactional //开启事务  注解打在实现方法上
    public Long  SaveData(){

        //这里1表 需要用到2表的id 所以 通过IdWorker 先获取 雪花id， 如果入库之前不需要用到id  可以不用赋值实体id 底层已经配置自动填充 雪花id
        var id = IdWorker.getId();
        var store  = new Demo1();
        store.setStoreName("商户名称");
        store.setMerchantNo("843XXXX");
        store.setFacilitatorId(id);
        var fac = new Demo2();
        fac.setId(id);
        fac.setFacilitatorName("代理商名称");
        fac.setFacilitatorNo(111);
        demo2Mappere.insert(fac);
        //var ss = 10/0;
        baseMapper.insert(store);
        return  fac.getId();
    }


}
